package com.oneinlet

import org.junit.Test
import java.io.File
import java.io.FileOutputStream
import java.net.URL

/**
 * Created by WangZiHe on 2018/5/19
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */

class TestYCHttp {

    @Test
    fun testDoGet() {
        // 参数独立于map中
        val getUrl = "http://localhost:8080/cinema/account/selectAccount"
        val map = mapOf("userId" to "1")
        val result = YCHttp.doGet(getUrl, map)
        println("result: $result")

        // 参数和url放在一起
        val getUrlParameter = "http://localhost:8080/cinema/account/selectAccount?userId=1"
        val result1 = YCHttp.doGet(getUrlParameter)
        println("result1: $result1")
    }


    @Test
    fun testDoPostJSON() {
        val url = "http://localhost:8080/cinema/user/register"
        val json = "  {\n" +
                "  \t\"user\":{\n" +
                "  \t\t\"age\": 19,\n" +
                "        \"email\": \"648830605@qq.com\",\n" +
                "        \"id\": 1,\n" +
                "        \"nickName\": \"王子和\",\n" +
                "        \"password\": \"123456\",\n" +
                "        \"phone\": \"15927961644\",\n" +
                "        \"sex\": \"男\"\n" +
                "  \t}\n" +
                "  \t\n" +
                "  }"
        val result = YCHttp.doPostJSON(url, json)
        println("result1: $result")
    }

    @Test
    fun testDoPostXML() {
        val url = "http://localhost:8080/cinema/account/test"
        val xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>\n" +
                "  <user>\n" +
                "    <age>19</age>\n" +
                "    <email>648830605@qq.com</email>\n" +
                "    <id>1</id>\n" +
                "    <nickName>王子和</nickName>\n" +
                "    <password>123456</password>\n" +
                "    <phone>15927961644</phone>\n" +
                "    <sex>男</sex>\n" +
                "  </user>\n" +
                "</root>"
        val result = YCHttp.doPostXML(url, xml)
        println("result1: $result")
    }

    @Test
    fun testDoPostFormUrl() {
        val url = "http://localhost:8080/cinema/account/test"
        val formUrlData = "name=520code&age=3&address=中国深圳"
        val result = YCHttp.doPostFormUrl(url, formUrlData)
        println("result: $result")

        val url1 = "http://localhost:8080/cinema/account/test"
        val map = mapOf("name" to "520", "age" to "3", "address" to "中国深圳")
        val result1 = YCHttp.doPostFormUrl(url1, map)
        println("result1: $result1")
    }


    @Test
    fun testDoPostFormData() {
        val url = "http://localhost:8080/cinema/account/upload"
        val map = mapOf("name" to "王子和", "age" to "25", "address" to "湖北省武汉市")
        val file1 = File("C:\\20180510221218.wav")
        val file2 = File("C:\\微信图片_20170825200817.jpg")
        val mapFile = mapOf("file1" to file1, "file2" to file2)
        val result1 = YCHttp.doPostFormData(url, map, mapFile)
        println("result1: $result1")
    }

    @Test
    fun testDoPostFormData01() {
        val url = "http://localhost:8094/static/test"
        val map = mapOf("dev" to "test", "age" to "25", "address" to "湖北省武汉市")
        val file = File("/home/yancheng/Desktop/dist(5).zip")
        val mapFile = mapOf("file" to file)
        val result1 = YCHttp.doPostFormData(url, map, mapFile)
        println("result1: $result1")
    }


    @Test
    fun testDoPostJSONSaveUser() {
        val url = "http://localhost:8085/user/save"
        val json = "  {\n" +
                "  \t\"user\":{\n" +
                "  \t\t\"age\": 19,\n" +
                "        \"id\": 1,\n" +
                "        \"username\": \"王子和123\",\n" +
                "        \"password\": \"123456\",\n" +
                "        \"address\": \"广东省深圳市\"\n" +
                "  \t}\n" +
                "  \t\n" +
                "  }"
        val result = YCHttp.doPostJSON(url, json)
        println("result1: $result")
    }


    @Test
    fun testDoPostJSONSaveUser001() {

        val url = "https://www.easy-mock.com/mock/5b1cd29dedd2ca7068a7db91/example/upload_copy_1528615830722"
        val json = "  {\n" +
                "  \t\"user\":{\n" +
                "  \t\t\"age\": 19,\n" +
                "        \"id\": 1,\n" +
                "        \"username\": \"王子和123\",\n" +
                "        \"password\": \"123456\",\n" +
                "        \"address\": \"广东省深圳市\"\n" +
                "  \t}\n" +
                "  \t\n" +
                "  }"

        val header = mapOf("token" to "123456", "auth" to "王子和")
        val result = YCHttp.doPostJSON(url, json, header)
        println("result1: $result")
    }


    @Test
    fun testDoGetInputStream() {
        val url = "http://p.64365.com/hzlawyer/3298607/20181025172235168237.jpg"
        val inputStreamImpl = YCHttp.doGetInputStream(url)
        val file = File("C:\\Users\\Administrator\\Desktop\\9995559.jpg")
        /*if (file.exists()) {
            file.createNewFile()
        }*/
        val fileOutputStream = FileOutputStream(file)
        val count = YCStream.write(inputStreamImpl, fileOutputStream)
        print(YCFormat.formatSpaceSize(count))
    }

    @Test
    fun testDoGetAppendHeader() {
        val url = "https://daluumini.daluu.com/coupon/listMyCoupona"
        val requestDataMap = mapOf("page" to "0", "canUse" to "true")
        val headerMap = mapOf("Authorization" to "87605e829806d0c118e4aa5b924525f1")
        val result = YCHttp.doGet(url, requestDataMap, headerMap)
        println(result)
    }


}